﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Extentions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json.Linq;
using TrainingReminderSystem.Bll;
using TrainingReminderSystem.Model;

namespace TrainingReminderSystem.ApiControllers
{
    /// <summary>
    /// 系统基础模块
    /// </summary>
    public partial class SystemController : BaseControllerBase
    {
        #region Excel转换成权限数据表并初始化角色 角色权限管理

        /// <summary>
        /// excel转换成权限数据表并初始化角色
        /// 二级菜单不能合并,权限ID可为空字符串
        /// </summary>
        /// <param name="form">form对象</param>
        /// <returns></returns>
        [Route("api/System/InitPower")]
        [ExOnlyValidateSysEmployee]
        [HttpPost]
        [ExOperateLog(EnumOperateLogType.初始化权限)]
        public AjaxResult<dynamic> InitPower([FromForm] IFormCollection form)
        {
            SysRolePostBll bll = new SysRolePostBll();
            var result = bll.InitPower(form);
            return result;
        }

        [Route("api/System/AllMenu")]
        [HttpPost]
        public AjaxResult<List<SysRolePostBll.MenuClass>> AllMenu()
        {
            var result = new SysRolePostBll().GetAllMenu(null, false);
            return AjaxResult<List<SysRolePostBll.MenuClass>>.SuccessResult(result);
        }

        [Route("api/System/SysRolePostList")]
        [HttpPost]
        public AjaxResult<dynamic> SysRolePostList([FromForm] IFormCollection form)
        {
            var bll = new SysRolePostBll();
            AjaxResult<dynamic> r = bll.GetListByPage(form["index"].ExToInt32(), form["size"].ExToInt32(), form);
            return r;
        }

        [Route("api/System/SysRolePostListSelect")]
        [HttpPost]
        public AjaxResult<dynamic> SysRolePostListSelect([FromForm] IFormCollection form)
        {
            var bll = new SysRolePostBll();
            AjaxResult<dynamic> r = bll.GetListSelect(form);
            return r;
        }

        [Route("api/System/SysRolePostAdd")]
        [HttpPost]
        [ExOperateLog(EnumOperateLogType.权限角色新建编辑)]
        public AjaxResult<dynamic> SysRolePostAdd(SysRolePost model)
        {
            var bll = new SysRolePostBll();
            AjaxResult<dynamic> r = bll.Save(model);
            return r;
        }

        #endregion

        #region 数据库相关

        /// <summary>
        /// EF数据库检测
        /// </summary>
        /// <returns></returns>
        [Route("api/System/CheckDb")]
        [ExOnlyValidateSysEmployee]
        [HttpPost]
        public AjaxResult<dynamic> CheckDb()
        {
            SysRolePostBll bll = new SysRolePostBll();
            var result = bll.CheckDb();
            return result;
        }

        #endregion

        #region 员工管理

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [Route("api/System/UpdateEmployeePwd")]
        [HttpPost]
        [ExOperateLog(EnumOperateLogType.重置员工密码)]
        public AjaxResult<int> UpdateEmployeePwd([FromForm] IFormCollection form)
        {
            var bll = new SysEmployeeBll();
            AjaxResult<int> result = bll.UpdatePwd(form["id"].ExToInt32(), null, form["pass"], true);
            return result;
        }

        /// <summary>
        /// 员工列表
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [Route("api/System/SysEmployeeList")]
        [HttpPost]
        public AjaxResult<dynamic> SysEmployeeList([FromForm] IFormCollection form)
        {
            var bll = new SysEmployeeBll();
            var result = bll.GetListByPage(form["index"].ExToInt32(), form["size"].ExToInt32(), form);
            return result;
        }

        [Route("api/System/SysEmployeeListSelect")]
        [HttpPost]
        public AjaxResult<dynamic> SysEmployeeListSelect([FromForm] IFormCollection form)
        {
            var bll = new SysEmployeeBll();
            var result = bll.GetListSelect(form);
            return result;
        }

        [Route("api/System/SysEmployeeAdd")]
        [HttpGet]
        public AjaxResult<dynamic> SysEmployeeAdd(int id)
        {
            var bll = new SysEmployeeBll();
            var dbModel = bll.GetModel(id);
            return dbModel;
        }

        /// <summary>
        /// 新建/编辑员工
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("api/System/SysEmployeeAdd")]
        [ExOperateLog(EnumOperateLogType.员工新建编辑)]
        [HttpPost]
        public AjaxResult<dynamic> SysEmployeeAdd(SysEmployee model)
        {
            var bll = new SysEmployeeBll();
            var r = bll.Save(model);
            return r;
        }

        #endregion

        #region 系统设置

        /// <summary>
        /// 系统设置
        /// </summary>
        /// <param name="enumSettingType"></param>
        /// <returns></returns>
        [Route("api/System/SysSettingList")]
        [HttpPost]
        public AjaxResult<List<SysSetting>> SysSettingList(EnumSettingType enumSettingType = EnumSettingType.系统参数)
        {
            var bll = new SysSettingBll();
            AjaxResult<List<SysSetting>> r = bll.GetListByType(enumSettingType);
            return r;
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("api/System/SysSettingSave")]
        [ExOperateLog(EnumOperateLogType.系统参数保存)]
        [HttpPost]
        public AjaxResult<dynamic> SysSettingSave(SysSetting model)
        {
            var bll = new SysSettingBll();
            var r = bll.Save(model);
            return r;
        }

        #endregion

        #region 修改个人信息

        /// <summary>
        /// 个人信息
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [Route("api/System/MyInfo")]
        [ExOnlyValidateSysEmployee]
        [HttpGet]
        public AjaxResult<dynamic> MyInfo()
        {
            var bll = new SysEmployeeBll();
            return bll.GetMyInfo();
        }

        /// <summary>
        /// 修改个人信息
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [Route("api/System/MyInfo")]
        [ExOnlyValidateSysEmployee]
        [HttpPost]
        [ExOperateLog(EnumOperateLogType.修改我的信息)]
        public AjaxResult<dynamic> MyInfo([FromForm] IFormCollection form)
        {
            var bll = new SysEmployeeBll();
            var result = bll.UpdateMyInfo(form["SysEmployeeName"], form["SysEmployeePhone"], form["HeaderUrl"]);
            return result;
        }

        #endregion

        #region 修改密码

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [Route("api/System/UpdatePwd")]
        [ExOnlyValidateSysEmployee]
        [HttpPost]
        [ExOperateLog(EnumOperateLogType.修改我的密码)]
        public AjaxResult<int> UpdatePwd([FromForm] IFormCollection form)
        {
            var bll = new SysEmployeeBll();
            int empID = PcLoginedBll.CurrentEmployee.SysEmployeeID;
            var result = bll.UpdatePwd(empID, form["oldPass"], form["pass"]);
            return result;
        }

        #endregion

        #region 店铺管理

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [Route("api/System/SysStoreList")]
        [HttpPost]
        public async Task<AjaxResult<dynamic>> SysStoreList([FromForm] IFormCollection form)
        {
            var bll = new SysStoreBll();
            var result = await bll.GetListByPage(PageIndex(form), PageSize(form), form);
            return result;
        }

        /// <summary>
        /// 得到对象信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("api/System/SysStoreAdd")]
        [HttpGet]
        public async Task<AjaxResult<dynamic>> SysStoreAdd(int id)
        {
            var bll = new SysStoreBll();
            var ar = await bll.GetModel(id);
            dynamic model = ar.Result;
            return AjaxResult<dynamic>.SuccessResult(model);
        }

        /// <summary>
        /// 新建/编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("api/System/SysStoreAdd")]
        [HttpPost]
        [ExOperateLog(EnumOperateLogType.店铺新建编辑)]
        public async Task<AjaxResult<dynamic>> SysStoreAdd(SysStore model)
        {
            var bllModel = new SysStore();
            bllModel = bllModel.ExCopyFrom(model);
            var bll = new SysStoreBll();
            var r = await bll.Save(bllModel);
            return r;
        }

        ///// <summary>
        ///// 删除
        ///// </summary>
        ///// <param name="form"></param>
        ///// <returns></returns>
        //[Route("api/System/SysStoreDel")]
        //[HttpPost]
        //[ExOperateLog(EnumOperateLogType.店铺删除)]
        //public async Task<AjaxResult<dynamic>> SysStoreDel([FromForm] IFormCollection form)
        //{
        //    var bll = new SysStoreBll();
        //    var r = await bll.Del(form["id"].ExToInt32());
        //    return r;
        //}

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [Route("api/System/SysStoreUpdateEnumStatus")]
        [HttpPost]
        [ExOperateLog(EnumOperateLogType.店铺修改状态)]
        public async Task<AjaxResult<dynamic>> SysStoreUpdateEnumStatus([FromForm] IFormCollection form)
        {
            var bll = new SysStoreBll();
            var r = await bll.UpdateEnumStatus(form["id"].ExToInt32(), form["status"].ExToInt32());
            return r;
        }

        /// <summary>
        /// 下拉查询
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [Route("api/System/SysStoreListSelect")]
        [ExOnlyValidateSysEmployee]
        [HttpPost]
        public async Task<AjaxResult<dynamic>> SysStoreListSelect([FromForm] IFormCollection form)
        {
            var bll = new SysStoreBll();
            var result = await bll.GetListSelect(form);
            return result;
        }

        #endregion

        #region 数据字典管理

        /// <summary>
        /// 数据字典列表
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [Route("api/System/SysDicList")]
        [HttpPost]
        public AjaxResult<dynamic> SysDicList([FromForm] IFormCollection form)
        {
            var bll = new SysDicBll();
            var result = bll.GetListByPage(form["index"].ExToInt32(), form["size"].ExToInt32(), form, "OrderID", true);
            return result;
        }

        /// <summary>
        /// 数据字典下拉
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [Route("api/System/SysDicListSelect")]
        [ExOnlyValidateSysEmployee]
        [HttpPost]
        public AjaxResult<dynamic> SysDicListSelect([FromForm] IFormCollection form)
        {
            var bll = new SysDicBll();
            var result = bll.GetListSelect(form);
            return result;
        }

        /// <summary>
        /// 新增字典
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("api/System/SysDicAdd")]
        [HttpPost]
        [ExOperateLog(EnumOperateLogType.字典新建编辑)]
        public AjaxResult<dynamic> SysDicAdd(SysDic model)
        {
            var bll = new SysDicBll();
            var r = bll.Save(model);
            return r;
        }

        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [Route("api/System/SysDicDel")]
        [HttpPost]
        [ExOperateLog(EnumOperateLogType.字典新建编辑)]
        public AjaxResult<dynamic> SysDicDel([FromForm] IFormCollection form)
        {
            var bll = new SysDicBll();
            var r = bll.Del(form["id"].ExToInt32());
            return r;
        }

        /// <summary>
        /// 字典排序
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [Route("api/System/SysDicOrder")]
        [HttpPost]
        public AjaxResult<int> SysDicOrder([FromForm] IFormCollection form)
        {
            string idsString = form["ids"];
            List<int> ids = idsString.ExToSplitList();
            return new SysDicBll().Order(ids);
        }

        #endregion

        #region 特殊导出

        /// <summary>
        /// sql语句名称
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [Route("api/System/SysStatisticalSqlList")]
        [HttpPost]
        public AjaxResult<dynamic> SysStatisticalSqlList([FromForm] IFormCollection form)
        {
            var bll = new SysStatisticalSqlBll();
            var result = bll.GetListByPage(form["index"].ExToInt32(), 1000, form);
            return result;
        }

        [Route("api/System/SysStatisticalSqlAdd")]
        [HttpPost]
        [ExOperateLog(EnumOperateLogType.特殊导出新建编辑)]
        public AjaxResult<dynamic> SysStatisticalSqlAdd(SysStatisticalSql model)
        {
            var bll = new SysStatisticalSqlBll();
            var r = bll.Save(model);
            return r;
        }

        [Route("api/System/SysStatisticalSqlDel")]
        [HttpPost]
        [ExOperateLog(EnumOperateLogType.特殊导出删除)]
        public AjaxResult<dynamic> SysStatisticalSqlDel([FromForm] IFormCollection form)
        {
            var bll = new SysStatisticalSqlBll();
            var r = bll.Del(form["id"].ExToInt32());
            return r;
        }

        [Route("api/System/SearchBySql")]
        [HttpPost]
        public AjaxResult<dynamic> SearchBySql([FromForm] IFormCollection form)
        {
            var bll = new SysStatisticalSqlBll();
            var r = bll.SearchBySql(form["id"].ExToInt32(), form["topVal"].ExToInt32(), form["canOrderBy"].ExToBool(), form["asc"].ExToBool(), form["orderBy"]);
            return r;
        }

        [Route("api/System/TotalBySql")]
        [HttpPost]
        public AjaxResult<int> TotalBySql([FromForm] IFormCollection form)
        {
            var bll = new SysStatisticalSqlBll();
            var r = bll.TotalBySql(form["id"].ExToInt32());
            return r;
        }

        [Route("api/System/ExportBySql")]
        [HttpPost]
        public AjaxResult<string> ExportBySql([FromForm] IFormCollection form)
        {
            var bll = new SysStatisticalSqlBll();
            var r = bll.ExportBySql(form["id"].ExToInt32());
            return r;
        }

        #endregion

        #region 附件管理

        /// <summary>
        /// 附件查看
        /// 只要是员工登录,就能查看附件
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [Route("api/System/SysFileList")]
        [HttpPost]
        [ExOnlyValidateSysEmployee]
        public async Task<AjaxResult<dynamic>> SysFileList([FromForm] IFormCollection form)
        {
            var bll = new SysFileBll();
            var result = await bll.GetListByPage(form["index"].ExToInt32(), form["size"].ExToInt32(), form);
            return result;
        }

        /// <summary>
        /// 附件上传
        /// 只要是员工登录,就能上传附件
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("api/System/SysFileAdd")]
        [HttpPost]
        [ExOnlyValidateSysEmployee]
        public async Task<AjaxResult<dynamic>> SysFileAdd(SysFile model)
        {
            var bll = new SysFileBll();
            var result = await bll.Save(model);
            return result;
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [Route("api/System/UploadFile")]
        [HttpPost]
        [ExOnlyValidateSysEmployee]
        public async Task<AjaxResult<dynamic>> UploadFile([FromForm] IFormCollection form)
        {
            if (form == null || form.Files.Count == 0)
            {
                return AjaxResult<dynamic>.ErrorResult("没有获取到上传文件");
            }
            IFormFile file = form.Files[0];
            string md5 = string.Empty;
            string localFile = string.Empty;
            string filePath = Base64Helper.SaveIFormFile(file, ref md5, ref localFile, SysFileBll.FilePath, false);
            if (string.IsNullOrEmpty(filePath))
            {
                return AjaxResult<dynamic>.ErrorResult("不支持的上传文件");
            }
            var bll = new SysFileBll();
            var fileName = file.FileName;
            SysFile model = new SysFile()
            {
                FileMD5 = md5,
                FilePath = filePath,
                SysFileName = fileName,

                UploadFileLocalFile = localFile
            };
            var result = await bll.Save(model);
            return result;
        }

        [Route("api/System/SysFileUpdateName")]
        [HttpPost]
        public AjaxResult<int> SysFileUpdateName([FromForm] IFormCollection form)
        {
            var bll = new SysFileBll();
            var result = bll.SysFileUpdateName(form);
            return result;
        }

        [Route("api/System/SysFileDel")]
        [HttpPost]
        public async Task<AjaxResult<dynamic>> SysFileDel([FromForm] IFormCollection form)
        {
            var bll = new SysFileBll();
            var result = await bll.Del(form["id"].ExToInt32());
            return result;
        }

        #endregion

        #region 微信相关设置

        /// <summary>
        /// 微信客户端相关设置
        /// </summary>
        /// <returns></returns>
        [Route("api/System/Weixin")]
        [HttpPost]
        public AjaxResult<string> Weixin()
        {
            string url = WxOpenSdkHelper.GetManageUrl(EnumWxType.客户端, EnumOpenIDType.微信公众号, EnumDealerShopType.子店铺, PcLoginedBll.CurrentStore.GetParentID().ToString());
            return AjaxResult<string>.SuccessResult(url);
        }

        /// <summary>
        /// 微信客户端小程序相关设置
        /// </summary>
        /// <returns></returns>
        [Route("api/System/WeixinMini")]
        [HttpPost]
        public AjaxResult<string> WeixinMini()
        {
            string url = WxOpenSdkHelper.GetManageUrl(EnumWxType.客户端, EnumOpenIDType.微信小程序, EnumDealerShopType.子店铺, PcLoginedBll.CurrentStore.GetParentID().ToString());
            return AjaxResult<string>.SuccessResult(url);
        }

        /// <summary>
        /// 后台网站微信
        /// </summary>
        /// <returns></returns>
        [Route("api/System/WeixinPc")]
        [HttpPost]
        public AjaxResult<string> WeixinPc()
        {
            string url = WxOpenSdkHelper.GetManageUrl(EnumWxType.管理后台, EnumOpenIDType.微信公众号, EnumDealerShopType.主店铺);
            return AjaxResult<string>.SuccessResult(url);
        }

        #endregion

        #region 问题反馈

        /// <summary>
        /// 员工列表
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [Route("api/System/SysFeedbackList")]
        [HttpPost]
        public async Task<AjaxResult<dynamic>> SysFeedbackList([FromForm] IFormCollection form)
        {
            var bll = new SysFeedbackBll();
            var result = await bll.GetListByPage(form["index"].ExToInt32(), form["size"].ExToInt32(), form);
            return result;
        }

        [Route("api/System/SysFeedbackRecordList")]
        [HttpPost]
        public async Task<AjaxResult<dynamic>> SysFeedbackRecordList([FromForm] IFormCollection form)
        {
            var bll = new SysFeedbackBll();
            var r = await bll.GetListRecord(form["index"].ExToInt32(), form["size"].ExToInt32(), form);
            return r;
        }

        [Route("api/System/SysFeedbackAddRecord")]
        [HttpPost]
        public async Task<AjaxResult<dynamic>> SysFeedbackAddRecord(SysFeedbackRecord model)
        {
            var bll = new SysFeedbackBll();
            var dbModel = await bll.AddRecord(model);
            return dbModel;
        }

        #endregion

        #region  打印相关

        //public ActionResult Print(int PrintTemplateID, string[] FormatString)
        //{
        //    return new PrintTemplateBll().Print(PrintTemplateID, FormatString).ResultContent();
        //}

        //public ActionResult GetPrintTemplateByTypeID(EnumPrintType? type)
        //{
        //    return new PrintTemplateBll().GetPrintTemplateByTypeID(type).ResultContent();
        //}

        #endregion
    }
}
